package com.ycsj.common.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.ycsj.common.R;
import com.ycsj.common.utils.ScreenUtils;
import com.ycsj.common.utils.UiUtils;

import java.util.List;


/**
 * 相册选择图片GirdView展示的适配器
 * Created by Administrator on 2018/1/17 0017.
 */

public class ShowPicGvAdapter extends BaseAdapter {
    private Context context;
    private List<String> picPaths;

    public ShowPicGvAdapter(Context context, List<String> picPaths) {
        this.context = context;
        this.picPaths = picPaths;
    }

    @Override
    public int getCount() {
        if (picPaths.size() == 9) {
            return 9;
        } else {
            return picPaths.size() + 1;
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {
            convertView = View.inflate(context, R.layout.base_common_gv_item_dynamic, null);
            holder = new ViewHolder(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        //动态计算条目的宽度,注意这里转化成px
        int itemWidth = (ScreenUtils.getScreenWidth(context) - UiUtils.dp2px(8 * 4 + 8 * 3)) / 4;

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
        holder.lvImg.setLayoutParams(lp);

        //做动态处理,根据返回的图片途径来显示图片
        if (picPaths.size() == 9) {
            //只显示图片,不显示加号
            Glide.with(context).load(picPaths.get(position)).into(holder.lvImg);
            holder.lvDelete.setVisibility(View.VISIBLE);
        } else {

            //显示图片和加号,最后一个位置显示+号
            if (position != picPaths.size()) {
                //不是最后一个位置
                Glide.with(context).load(picPaths.get(position)).into(holder.lvImg);
                holder.lvDelete.setVisibility(View.VISIBLE);
            } else {
                //最后一个位置
                Glide.with(context).load(R.drawable.base_add_img).into(holder.lvImg);
                holder.lvDelete.setVisibility(View.GONE);
            }
        }


        //删除按钮的点击事件
        holder.lvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                picPaths.remove(position);
                notifyDataSetChanged();
            }
        });
        return convertView;
    }

    static class ViewHolder {
        ImageView lvImg;
        ImageView lvDelete;
        RelativeLayout rlOut;

        ViewHolder(View view) {
            lvImg = view.findViewById(R.id.lvImg);
            lvDelete = view.findViewById(R.id.lvDelete);
            rlOut = view.findViewById(R.id.rl_out);
            view.setTag(this);
        }
    }

}
